'''
T1
（1）整数和ASCII互转: 求数字68对应的ASCII码字符， 求字符F的ASCII码值
（2）以每行8个字符打印 ASCII码表 （ascii码值100以内的）
'''

# 整数和ASCII互转
# 数字68对应的ASCII码字符
ascii_char_from_number = chr(68)
print(f"数字68对应的ASCII码字符是: {ascii_char_from_number}")

# 字符F的ASCII码值
ascii_value_from_char = ord('F')
print(f"字符F的ASCII码值是: {ascii_value_from_char}")

# 打印ASCII码表，每行8个字符，ASCII码值100以内
for i in range(32, 100):  # 从32开始，因为32之前的ASCII码通常是控制字符
    print(f"{chr(i):3}", end=" ")
    if (i - 32) % 8 == 7:
        print()  # 每8个字符换行

'''
T2
打印99乘法表
'''
for i in range(1, 10):
    for j in range(1, i + 1):
        print(f"{j} * {i} = {i * j:2}", end="\t")
    print() 


'''
T3
判断一个句话（一个字符串）"Read as much as you can in English. No matter what it is, from cartoons to British literature anything counts."中是否包含下面单词列表中的任何一个单词：
how
are
good
new
benifit
no
'''
sentence = "Read as much as you can in English. No matter what it is, from cartoons to British literature anything counts."
word_list = ["how", "are", "good", "new", "benifit", "no"]
# 将句子转换为小写并分割成单词列表
words_in_sentence = sentence.lower().split()
# 检查句子中是否包含单词列表中的任何一个单词
contains_any_word = any(word in words_in_sentence for word in word_list)
if contains_any_word:
    print("句子中包含单词列表中的至少一个单词。")
else:
    print("句子中不包含单词列表中的任何一个单词。")

'''
T4
（1）找一个自己电脑上文件较多、文件夹层数较多的工作文件夹，分别打印列出该文件夹（及其子文件夹）中的word, ppt, 和 excel文件的绝对路径；
（2）装上述word, ppt, 和 excel分别复制到一个独立的文件夹中。
'''
import os

def find_files_with_extensions(root_dir, extensions):
    matching_files = []
    for dirpath, _, filenames in os.walk(root_dir):
        for filename in filenames:
            if any(filename.lower().endswith(ext) for ext in extensions):
                matching_files.append(os.path.abspath(os.path.join(dirpath, filename)))
    return matching_files

# 指定工作文件夹路径
work_dir = r''  # 请替换为你的工作文件夹路径
extensions = ['.doc', '.docx', '.ppt', '.pptx', '.xls', '.xlsx']

# 找到所有匹配的文件
matching_files = find_files_with_extensions(work_dir, extensions)

# 打印匹配的文件路径
for file_path in matching_files:
    print(file_path)

import shutil

def copy_files_to_folder(source_files, target_dir):
    if not os.path.exists(target_dir):
        os.makedirs(target_dir)
    
    for file_path in source_files:
        file_name = os.path.basename(file_path)
        target_path = os.path.join(target_dir, file_name)
        shutil.copy2(file_path, target_path)
        print(f"Copied {file_path} to {target_path}")

# 指定目标文件夹路径
target_folder = r''  # 请替换为你的目标文件夹路径

# 复制文件到目标文件夹
copy_files_to_folder(matching_files, target_folder)

'''
T5
批量修改文件后缀
将某一个文件夹下的所有.doc后缀的文件的文件名加上前缀"DOC_"进行重命名。
'''
import os

def rename_files_with_prefix(root_dir, extension, prefix):
    for dirpath, _, filenames in os.walk(root_dir):
        for filename in filenames:
            if filename.lower().endswith(extension):
                old_file_path = os.path.join(dirpath, filename)
                new_filename = prefix + filename
                new_file_path = os.path.join(dirpath, new_filename)
                os.rename(old_file_path, new_file_path)
                print(f"Renamed {old_file_path} to {new_file_path}")

# 指定工作文件夹路径
work_dir = r''  # 请替换为你的工作文件夹路径
extension = '.doc'
prefix = 'DOC_'

# 批量重命名文件
rename_files_with_prefix(work_dir, extension, prefix)